home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / LIBRARY / PBLIB1 / PROGS / SCRNGEN.PAS < prev    next >
Pascal/Delphi Source File  |  1994-05-03  |  19KB  |  613 lines

  1. Program SCRNGen;
  2.  
  3. {$M 20000,0,50000}
  4.  
  5. uses PbMISC, PbDATA, PbOBJS, PbPARMS, PbDDL, PbSCRN;
  6.  
  7.  
  8. {
  9. Description:  Starting point for program to generate UNITs
  10.  
  11. Author      : Howard Richoux
  12. Date        : 2/5/94
  13. Last revised: 1.10  2/8/94  still early development
  14.               1.12  2/18/94 NEW LIBRARIES
  15. Application : IBM PC and compatibles, done in Turbo Pascal 7.0
  16. Status      : Placed in the Public Domain by HNR Software 1/29/94
  17. Published in: none
  18.  
  19. Intended to be the starting point for future programs like DBPASGEN and BFILEGEN.
  20.  
  21. This is oriented to producing Units which are essentially OBJECTS with
  22.   the appropriate PASCAL shell around them.
  23.  
  24. }
  25.  
  26.  
  27.  
  28.  
  29. var OUTPUTname     : string[40]; { file name for OUTPUT program }
  30. var INPUTname      : string[40]; { file name for SOURCE data }
  31. var INPUText       : string[3];  { default file ext for SOURCE data }
  32. var root           : string[7];  { sort of a central identifier for fields, ... }
  33. var prefix         : string[1];  { like x or z --> "xNAME.pas" }
  34. var AncestorObject : string;     { the object this is derived from }
  35. var CurrentObject  : string;     { THIS OBJECT }
  36. var FieldSpec      : string;     { useful "[fld1(s10,fld2(r10.2)]"  }
  37.  
  38. var OUTPT          : OUT_object_0; { Lines are output to FILE }
  39. var L              : STRA_object;  { Lines are held here until dumped }
  40.  
  41.  
  42.  
  43. { MAIN Code }
  44.  
  45.  
  46. Procedure LogCRTfile(fn,sect : string);
  47. var i : integer;
  48.      begin
  49.      L.append('{');
  50.      L.append('Screen source file  ['+fn+'] ['+sect+']');
  51.      L.append('');
  52.      L.append('  Screen Dimensions   width: '+integerstr(scrnwidth,2)+
  53.              '   length: '+integerstr(scrnlength,2));
  54.      L.append('  Screen Labels         top:['+scrntoplabl+
  55.              ']   bottom:['+scrnbotlabl+']');
  56.      L.append(' ');
  57.      L.append('  Picture: ');
  58.      for i := 1 to image.count do
  59.           L.append(image.fetchN(i));
  60.      L.append('}');
  61.      end;
  62.  
  63.  
  64. Function FieldRootStr(nam : string) : string;
  65. var s : string;
  66.     i : integer;
  67.      begin
  68.      s := nam;
  69.      i := length(s);
  70.      while i > 0 do
  71.           begin
  72.           i := pos('.',s);
  73.           if i > 0 then delete(s,1,i);
  74.           end;
  75.      FieldRootStr := s;
  76.      end;
  77.  
  78.  
  79. Function CheckFieldOption(i : integer; option : string) : boolean;
  80.      {[FIELD] Check if "option" is present in opt string }
  81. var s,o,oo : string;
  82.      begin
  83.      CheckFieldOption := false;
  84.      s := flds.ddl[i].options;
  85.      trim(s); UpCaseStr(s);
  86.      oo := option;
  87.      trim(oo); UpCaseStr(oo);
  88.      while length(s) > 0 do
  89.           begin
  90.           o := GetLeftStr(s,',');
  91.           if ( o = oo ) then CheckFieldOption := true;
  92.           end;
  93.      end;
  94.  
  95.  
  96.  
  97. Procedure MakePasFields;
  98. var i,j,len,decp : integer;
  99.     s, nam,typstr  : string;
  100.     typ     : char;
  101.      begin
  102.      for j := 1 to flds.count do
  103.           begin
  104.           nam  := flds.ddl[j].nam;
  105.           typ  := flds.ddl[j].typ;
  106.           len  := flds.ddl[j].len;
  107.           decp := flds.ddl[j].decp;
  108.           typstr := '';
  109.           case typ of
  110.              'C' : begin                    {char array}
  111.                    if len > 1 then
  112.                         typstr := 'array[1..'+integerstr(len,3)+'] of char;'
  113.                    else typstr := 'char;';
  114.                    end;
  115.              'D' : typstr := 'string[8];';  {DBase Date}
  116.              'I' : typstr := 'integer;';    {integer}
  117.              'L' : typstr := 'longint;';    {longint}
  118.              'R' : typstr := 'real;';       {real}
  119.              'S' : begin                  {PASCAL string}
  120.                    if len = 0 then len := 1;
  121.                    if len > 1 then
  122.                         typstr := 'string['+integerstr(len,3)+'];'
  123.                    else typstr := 'char;';
  124.                    end;
  125.               else begin      {unknown}
  126.                    typstr := '{Unknown field type ['+typ+']}';
  127.                    len := 0;
  128.                    end;
  129.               end;
  130.           removeblanks(typstr);
  131.           L.append('          '+leftstr(nam,10)+': '+typstr);
  132.           end;
  133.      end;
  134.  
  135.  
  136. Procedure MakeVARData;
  137. var i : integer;
  138.      begin
  139.      L.append(' ');
  140.      if DeclareData then
  141.           begin
  142.           for i := 1 to flds.count do
  143.                L.append('var '+vars.fetchN(i));
  144.           end
  145.      else begin
  146.           L.append('{ Variables declared elsewhere'+ UsesStr+' }');
  147.           end;
  148.      L.append(' ');
  149.      end;
  150.  
  151.  
  152. Procedure MakeUnitStart;
  153. var i, width   : integer;
  154.     rtype      : char;
  155.     tmp, tpe   : string[40];
  156.      begin
  157.      L.append('{SECTION ..'+prefix+Root+' }');
  158.      L.append(' ');
  159.      L.append('{ '+pProgID+' - hnr   '+FormatDTime+
  160.               ', Placed in the Public Domain by HNR Software 1/94 }');
  161.      L.append(' ');
  162.      L.append('Unit '+prefix+Root+';');
  163.      L.append(' ');
  164.      L.append('INTERFACE');
  165.      L.append(' ');
  166.      L.append('Uses PbCRT, PbWIND, PbMISC, PbDATA, PbFIELDS '+UsesStr+';');
  167.      L.append(' ');
  168.      MakeVARData;
  169.      L.append(' ');
  170.      L.append(' ');
  171.      LogCRTfile(INPUTname,root);
  172.      L.append(' ');
  173.      end;
  174.  
  175.  
  176.  
  177. Function PbFIELDSectStr( nam : string; typ : char) : string;
  178. var s : string;
  179.      begin
  180.      s := '';
  181.      case typ of
  182.          'D'   : s := 'DBDATE';
  183.          'I'   : s := 'INTEGER';
  184.          'L'   : s := 'LONGINT';
  185.          'R'   : s := 'REAL';
  186.          'S'   : s := 'STRING';
  187.          else    s := 'UNKNOWN';
  188.          end;
  189.      PbFIELDSectStr := leftstr(FieldRootStr(nam)+'_fld',20)+' : '+s+'_FIELD_object;';
  190.      end;
  191.  
  192.  
  193. Procedure MakeObjectData;
  194. var i : integer;
  195.      begin
  196.      L.append('         '+'w           : WINDOW_object;');
  197.      L.append('         '+'readonly    : boolean;');
  198.      L.append('         '+'colorscheme : byte;');
  199.      L.append('         '+'exitcmd     : string[24];');
  200.      L.append('         '+'CRTSav      : CRTSaveRec; {Used only by POPUP}');
  201.      L.append(' ');
  202.      for i := 1 to flds.count do
  203.           begin
  204.           if CheckFieldOption(i,'DBDATE') then flds.ddl[i].typ := 'D';
  205.           L.append('         '+PbFIELDSectStr(flds.ddl[i].nam,flds.ddl[i].typ));
  206.           end;
  207.      L.append(' ');
  208.      end;
  209.  
  210.  
  211. Procedure ProcessFieldOptions(i : integer);
  212. var s,o : string;
  213.      begin
  214.      s := flds.ddl[i].options;
  215.      trim(s); UpCaseStr(s);
  216.      while length(s) > 0 do
  217.           begin
  218.           o := GetLeftStr(s,',');
  219.           if      o = 'DOLLAR3' then
  220.                        L.append('     '+ FieldRootStr(flds.ddl[i].nam)+'_fld.decp := 3;')
  221.           else if o = 'UPSHIFT' then
  222.                        L.append('     '+ FieldRootStr(flds.ddl[i].nam)+'_fld.SetUpShift;')
  223.           else if o = 'READONLY' then
  224.                        L.append('     '+ FieldRootStr(flds.ddl[i].nam)+'_fld.readonly := true;')
  225.           else if o = 'DBDATE' then begin {handled elsewhere} end
  226.           else begin
  227.                L.append('     { Unknown option ['+ o + '] }');
  228.                end;
  229.           end;
  230.      end;
  231.  
  232.  
  233. Procedure GenerateInitLine(i : integer);
  234.      begin
  235.      if flds.ddl[i].typ = 'R' then
  236.           begin
  237.           L.append('     r := ' + integerstr(flds.ddl[i].r,2) + '; '+
  238.                        ' c := ' + integerstr(flds.ddl[i].c,2) + '; '+
  239.                        FieldRootStr(flds.ddl[i].nam)+'_fld.init(r,c,' +
  240.                        integerstr(flds.ddl[i].l,2)+','+
  241.                        integerstr(flds.ddl[i].decp,2)+','+
  242.                        '''' + flds.ddl[i].prompt + '''' + ');' );
  243.           end
  244.      else begin
  245.           L.append('     r := ' + integerstr(flds.ddl[i].r,2) + '; '+
  246.                        ' c := ' + integerstr(flds.ddl[i].c,2) + '; '+
  247.                        FieldRootStr(flds.ddl[i].nam)+'_fld.init(r,c,' +
  248.                        integerstr(flds.ddl[i].l,2)+','+
  249.                        '''' + flds.ddl[i].prompt + '''' + ');' );
  250.           end;
  251.      ProcessFieldOptions(i);
  252.      end;
  253.  
  254.  
  255. Procedure MakeObjectInitProc(hdr : boolean);
  256. var i, width   : integer;
  257.     rtype      : char;
  258.     tmp,tmp2,tpe    : string[20];
  259.      begin
  260.      if hdr then
  261.           begin
  262.           L.append('         Procedure  init     ( x,y,color : byte);');
  263.           end
  264.      else begin
  265.           L.append(' ');
  266.           L.append('Procedure  '+CurrentObject+'.init( x,y,color : byte );');
  267.           L.append('var r,c : byte;');
  268.           L.append('     begin');
  269.           L.append('     exitcmd := ''?CONTINUE'';');
  270.           L.append('     readonly := false;');
  271.           L.append('     colorscheme := color;');
  272.           L.append('     SetColorScheme(colorscheme);');
  273.           L.append('     w.init(x,y,x+'+integerstr(Scrnwidth,2)+
  274.                                 ',y+'+integerstr(scrnlength,2)+',0);');
  275.           L.append('     w.SetLabels(''' +scrntoplabl + '''' + ',' +
  276.                                    + ''''+scrnbotlabl + '''' + ');');
  277.           for i := 1 to flds.count do
  278.                begin
  279.                GenerateInitLine(i);
  280.                end;
  281.           L.append('     end;');
  282.           L.append(' ');
  283.           L.append(' ');
  284.           end;
  285.      end;
  286.  
  287.  
  288. Procedure MakeObjectDoneProc(hdr : boolean);
  289. var i, width   : integer;
  290.     rtype      : char;
  291.     tmp,tmp2,tpe    : string[20];
  292.      begin
  293.      if hdr then
  294.           begin
  295.           L.append('         Procedure  done;');
  296.           end
  297.      else begin
  298.           L.append(' ');
  299.           L.append('Procedure  '+CurrentObject+'.done;');
  300.           L.append('     begin');
  301.           L.append('     w.done;');
  302.           L.append('     end;');
  303.           L.append(' ');
  304.           L.append(' ');
  305.           end;
  306.      end;
  307.  
  308.  
  309.  
  310. Procedure MakeObjectMethod2(hdr : boolean);
  311. var i, width   : integer;
  312.     s          : string;
  313.     rtype      : char;
  314.     tmp,tmp2,tpe    : string[20];
  315.      begin
  316.      if hdr then
  317.           begin
  318.           L.append('         Procedure  display;');
  319.           end
  320.      else begin
  321.           L.append(' ');
  322.           L.append('Procedure  '+CurrentObject+'.display;');
  323.           L.append('     begin');
  324.           L.append('     SetColorScheme(colorscheme);');
  325.           L.append('     w.drawframe;');
  326.           L.append('     w.clrscr;');
  327.           L.append('     PromptColor;');
  328.           for i := 2 to literals.count-1 do
  329.                begin
  330.                s := literals.fetchN(i);
  331.                if s <> '' then
  332.                      begin
  333.                      L.append('     DisplayStr('+ integerstr(i-1,2) + ',1,' +
  334.                                '''' + s + '''' + ');');
  335.                      end;
  336.                 end;
  337.           L.append('      DisplayData;');
  338.           L.append('      end;');
  339.           L.append(' ');
  340.           L.append(' ');
  341.           end;
  342.      end;
  343.  
  344.  
  345.  
  346. Procedure MakeObjectMethod1(hdr : boolean);
  347. var i, width   : integer;
  348.     s          : string;
  349.     rtype      : char;
  350.     tmp,tmp2,tpe    : string[20];
  351.      begin
  352.      if hdr then
  353.           begin
  354.           L.append('         Procedure  displaydata;');
  355.           end
  356.      else begin
  357.           L.append(' ');
  358.           L.append('Procedure  '+CurrentObject+'.displaydata;');
  359.           L.append('     begin');
  360.           L.append('     SetColorScheme(colorscheme);');
  361.           for i := 1 to flds.count do
  362.                begin
  363.                L.append('     '+ FieldRootStr(flds.ddl[i].nam) + '_fld.display('+
  364.                            flds.ddl[i].nam +');' );
  365.                end;
  366.           L.append('     end;');
  367.           L.append(' ');
  368.           L.append(' ');
  369.           end;
  370.      end;
  371.  
  372.  
  373.  
  374. Procedure MakeObjectMethod3(hdr : boolean);
  375. var i, width   : integer;
  376.     rtype      : char;
  377.     tmp,tmp2,tpe    : string[20];
  378.      begin
  379.      if hdr then
  380.           begin
  381.           L.append('         Procedure  input;');
  382.           end
  383.      else begin
  384.           L.append(' ');
  385.           L.append('Procedure  '+CurrentObject+'.input;');
  386.           L.append('var xit  : boolean;');
  387.           L.append('var next : integer;');
  388.           L.append('     begin');
  389.           L.append('     xit := false;');
  390.           L.append('     if readonly then');
  391.           L.append('          begin');
  392.           L.append('          xit := true;');
  393.           L.append('          PbCRT.pause;');
  394.           L.append('          end;');
  395.           L.append('     next := 1;');
  396.           L.append('     while not xit do');
  397.           L.append('          begin');
  398.           L.append('          case next of');
  399.           for i := 1 to flds.count do
  400.                begin
  401.                L.append('               '+ integerstr(i,2)+'   : '+
  402.                                   'if not xit then xit := '+
  403.                                    FieldRootStr(flds.ddl[i].nam) + '_fld.input('+
  404.                                    flds.ddl[i].nam +');' );
  405.                end;
  406.           L.append('               else   next := 0;');
  407.           L.append('               end;');
  408.           L.append('          if HKEY_LastTC = ''H'' then  {UpArrow } ');
  409.           L.append('               begin ');
  410.           L.append('               if next > 1 then dec(next); ');
  411.           L.append('               xit := false; HKEY_LastTC := '' '';');
  412.           L.append('               end');
  413.           L.append('          else if HKEY_LastTC = ''P'' then  {DownArrow } ');
  414.           L.append('               begin ');
  415.           L.append('               inc(next); ');
  416.           L.append('               xit := false; HKEY_LastTC := '' '';');
  417.           L.append('               end');
  418.           L.append('          else inc(next);');
  419.           L.append('          end;');
  420.           L.append('     ExitCmd := FunctionKeyDecode(HKEY_LastTC);');
  421.           L.append('     end;');
  422.           L.append(' ');
  423.           L.append(' ');
  424.           end;
  425.      end;
  426.  
  427.  
  428. Procedure MakeObjectMethod4(hdr : boolean);
  429. var i, width   : integer;
  430.     rtype      : char;
  431.     tmp,tmp2,tpe    : string[20];
  432.      begin
  433.      if hdr then
  434.           begin
  435.           L.append('         Procedure  PopUp    ( x,y,color : byte);');
  436.           end
  437.      else begin
  438.           L.append(' ');
  439.           L.append('Procedure  '+CurrentObject+'.PopUp( x,y,color : byte );');
  440.           L.append('var r,c : byte;');
  441.           L.append('     begin');
  442.           L.append('     SaveCRT(CRTSav);');
  443.           L.append('     init(x,y,color);');
  444.           L.append('     display;');
  445.           L.append('     input;');
  446.           L.append('     done;');
  447.           L.append('     RestoreCRT(CRTSav);');
  448.           L.append('     end;');
  449.           L.append(' ');
  450.           L.append(' ');
  451.           end;
  452.      end;
  453.  
  454.  
  455.  
  456. Procedure MakeObjectProcs(hdr : boolean);
  457.      begin
  458.      MakeObjectInitProc(hdr);
  459.      MakeObjectMethod1(hdr);
  460.      MakeObjectMethod2(hdr);
  461.      MakeObjectMethod3(hdr);
  462.      MakeObjectMethod4(hdr);
  463.      MakeObjectDoneProc(hdr);
  464.      end;
  465.  
  466.  
  467. Procedure MakeObjectHeader;
  468. var tmp   : string;
  469.      begin
  470.      L.append('{SECTION .'+Root+'_'+AncestorObject+' }');
  471.      L.append(' ');
  472.      tmp := 'OBJECT';
  473.      if AncestorObject <> '' then tmp := 'OBJECT('+AncestorObject+')';
  474.      L.append('type   '+CurrentObject+' = '+tmp);
  475.      MakeObjectData;
  476.      MakeObjectProcs(true);
  477.      L.append('         end;');
  478.      L.append(' ');
  479.      end;
  480.  
  481.  
  482. Procedure MakeImplementation;
  483.      begin
  484.      L.append(' ');
  485.      L.append('{SECTION .zImplementation }');
  486.      L.append('IMPLEMENTATION');
  487.      L.append(' ');
  488.      end;
  489.  
  490.  
  491. Procedure MakeUnitEnd;
  492.      begin
  493.      L.append(' ');
  494.      L.append('{SECTION zzInitialization }');
  495.      L.append('      begin { initialization }');
  496.      L.append('      end.');
  497.      end;
  498.  
  499.  
  500.  
  501. { ------------------------------------------------------------------- }
  502.  
  503. Procedure OUTSTRA(var L : STRA_object);
  504. var i : integer;
  505.     s : string;
  506.      begin
  507.      for i := 1 to L.count do
  508.           begin
  509.           s := L.fetchN(i);
  510.           OUTPT.OUT(s);
  511.           end;
  512.      end;
  513.  
  514.  
  515. Procedure GeneratePASCALCode;
  516. var outfname : string[40];
  517.      begin
  518.      L.init(500);
  519.      getdir(0,outfname);
  520.      outfname := addbackslash(outfname) + Prefix + Root;
  521.      forceext(outfname,'pas');
  522.      writeln('Writing to [',outfname,']');
  523.      OUTPT.LISTinit(outfname,OUT_typREWRITE);
  524.      OUTPT.LISTopen;
  525.  
  526.      MakeUnitStart;
  527.      MakeObjectHeader;
  528.      MakeImplementation;
  529.      MakeObjectProcs(false);
  530.      MakeUnitEnd;
  531.  
  532.      OUTSTRA(L);
  533.      OUTPT.done;
  534.      end;
  535.  
  536.  
  537. Procedure ProcessINPUTfile(fn : string);
  538.      begin
  539.      if fieldSpec <> '' then
  540.           begin
  541.           flds.init;
  542.           FieldSpecToPbDDL(FieldSpec,flds);
  543.           flds.dump;
  544.           end;
  545.      ProcessCRTFile(fn,root,flds);
  546.      {fields.dump;}
  547.      flds.dump;
  548.      end;
  549.  
  550.  
  551. Procedure DoSKELGen(OUTPUTname : string);
  552. var fn : string[40];
  553.      begin
  554.      fn := OUTPUTname;
  555.      writeln('fn ',fn);
  556.      writeln('root= ',Root);
  557.      forceext(INPUTname,'crt');
  558.      ProcessINPUTfile( INPUTname );
  559.      GeneratePASCALCode;
  560.      end;
  561.  
  562.  
  563. Procedure SKELGenInit;
  564.      begin
  565.      OUTPUTname := 'testunit.pas';   {Unit file to be generated}
  566.  
  567.      addparm(1,'SOURCE','');
  568.      addparm(1,'SOURCEEXT','txt');
  569.      addparm(1,'FILE','');
  570.      addparm(1,'FIELDS','');
  571.      addparm(1,'ROOT','');
  572.      addparm(1,'PREFIX','z');
  573.      addparm(1,'ANCESTOR','');
  574.      addparm(1,'USES','');
  575.  
  576.      StandardpVarsInit;
  577.  
  578.      prefix         := GetParmStr('PREFIX');
  579.      OUTPUTname     := GetParmStr('FILE');
  580.      INPUTname      := GetParmStr('SOURCE');
  581.      INPUText       := GetParmStr('SOURCEEXT');
  582.      UsesStr        := GetParmStr('USES');
  583.      AncestorObject := GetParmStr('ANCESTOR');
  584.  
  585.      Fieldspec      := GetParmStr('FIELDS');
  586.      Fieldspec      := UpCaseStr(FieldSpec);
  587.      trim(FieldSpec);
  588.      if FieldSpec[1] = '[' then RemoveEnds(FieldSpec);
  589.  
  590.      if paramcount > 0 then INPUTname := paramstr(1);
  591.  
  592.      root      := GetParmSTr('ROOT');
  593.      if root = '' then root := FileROOTStr(INPUTName);
  594.      root      := UpCaseStr(root);
  595.  
  596.      if AncestorObject <> '' then
  597.           CurrentObject := Root + '_' + AncestorObject
  598.      else CurrentObject := Root + '_object';
  599.      end;
  600.  
  601.  
  602.      begin
  603.      pProgID := 'SCRNGen 1.09';
  604.      writeln(pProgID, ' - Generate SCREEN Units - HNR 2/94');
  605.      SKELGenInit;
  606.      if INPUTname <> '' then
  607.           begin
  608.           DoSKELGen(INPUTname);
  609.           end
  610.      else writeln('Without specifying a SOURCE= file, there is no point in this exercise');
  611.      writeln('');
  612.      end.
  613.